<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="800">
    <el-form :model="form" ref="formRef" label-width="auto" style="max-width: 800px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">
          <template v-for="(item, index) in formCol">
            <template v-if="false"></template>
            <template v-else>
              <el-col :span="12">
                <el-form-item :label="item.label" :prop="item.key">
                  <el-input v-model="form[item.key]" :placeholder="`请输入${item.label}`" clearable />
                </el-form-item>
              </el-col>
            </template>
          </template>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
// 方法
import { ref, defineExpose, defineEmits } from 'vue'

// 接口
// import { addLaber, updateLaber } from '@/api/goods/label/index.js'

const instance = getCurrentInstance();
const { proxy } = instance || {};

// 显示表单
const dialogFormVisible = ref(false)
const title = ref('新增')
const isEdit = ref(false)
const formRef = ref(null)
// 表单
const form = ref({
  id: '',
  name: '', // 姓名
  phone: '', // 手机号
  idNumber: '', // 证件号
  visitingTime: '', // 拜访时间
  interviewee: '', // 被访人
  employeeId: '', // 被访人工号
  subject: '', // 事由
})

// 搜索列
const formCol = [
  { key: 'name', label: `访客姓名`, visible: true, children: [] },
  { key: 'phone', label: `手机号`, visible: true, children: [] },
  { key: 'idNumber', label: `证件号`, visible: true, children: [] },
  { key: 'visitingTime', label: `拜访时间`, visible: true, children: [] },
  { key: 'interviewee', label: `被访人`, visible: true, children: [] },
  { key: 'employeeId', label: `被访人工号`, visible: true, children: [] },
  { key: 'subject', label: `事由`, visible: true, children: [] },
]

// 验证
const rules = ref({
  name: [
    { required: true, message: '请填写名称', trigger: 'blur' }
  ]
})

const emits = defineEmits(['change'])

// 打开方法
const open = () => {
  for (let i in form.value) {
    form.value[i] = ''
  }
  dialogFormVisible.value = true
  isEdit.value = false
  title.value = '新增'
}
// 编辑
const edit = (data) => {
  for (let i in form.value) {
    form.value[i] = data[i]
  }
  isEdit.value = true
  dialogFormVisible.value = true
  title.value = '编辑'
}
// 关闭方法
const close = () => {
  dialogFormVisible.value = false
}

// 提交
const onSubmit = async () => {

  await formRef.value.validate((valid, fields) => {
    if (valid) {
      if(form.value.id) {
        updateLaber(form.value).then(res => {
          if(res.code === 200) {
            proxy.$modal.msgSuccess(title.value + '成功');
            close()
            emits('change')
          }
        })
      }else {
        addLaber(form.value).then(res => {
          if(res.code === 200) {
            proxy.$modal.msgSuccess(title.value + '成功');
            close()
            emits('change')
          }
        })
      }
    }
  })
}

defineExpose({
  open, close, edit
})

</script>

<style scoped lang="scss">

</style>
